
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>

/* Private function prototypes -----------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
void LED_Configuration(void);
void TIM3_TIMER_Config(void);
void TIM7_TIMER_Config(void);

#ifdef __GNUC__
  /* With GCC/RAISONANCE, Large printf (option LD Linker->Libraries->Large printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/***********************************************************
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	static uint32_t count;
	
	IAP_Init();
	
//	TIM3_TIMER_Config();
	TIM7_TIMER_Config();
	
	LED_Configuration();
	
	while(1){
		count++;
		
		if(count > 6000000){
			count = 0;
			GPIO_ResetBits(LED1_GPIO_PORT, LED1_PIN);
			GPIO_SetBits(LED2_GPIO_PORT, LED2_PIN);
			GPIO_SetBits(LED3_GPIO_PORT, LED3_PIN);
			GPIO_ResetBits(LED4_GPIO_PORT, LED4_PIN);
		}else if(count == 3000000){
			GPIO_ToggleBits(LED1_GPIO_PORT, LED1_PIN);
			GPIO_ToggleBits(LED2_GPIO_PORT, LED2_PIN);
			GPIO_ToggleBits(LED3_GPIO_PORT, LED3_PIN);
		}
	}
}

void TIM3_TIMER_Config(void)
{
  NVIC_InitTypeDef        NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
		
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Prescaler = 6 - 1;  // 60 MHz /  6 = 1 MHz
  TIM_TimeBaseStructure.TIM_Period = 10 - 1;    // 10 MHz / 10 = 1 MHz --> 1 us
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStructure);

	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  TIM_Cmd(TIM3, ENABLE);
}

void TIM7_TIMER_Config(void)
{
  NVIC_InitTypeDef        NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
		
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Prescaler = 6 - 1;  // 60 MHz /  6 = 1 MHz
  TIM_TimeBaseStructure.TIM_Period = 10 - 1;    // 10 MHz / 10 = 1 MHz --> 1 us
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM7,&TIM_TimeBaseStructure);

  NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStructure);

	TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);

  TIM_Cmd(TIM7, ENABLE);
}

/***********************************************************
  * @brief  LED_Configuration
  * @param  None
  * @retval None
  */
void LED_Configuration(void)
{
#ifdef LED_Enable
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Configure IAP_COM Tx as alternate function  */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

	RCC_AHB1PeriphClockCmd(LED1_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED1_PIN;
	GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED2_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED2_PIN;
	GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED3_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED3_PIN;
	GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStructure);
	
	RCC_AHB1PeriphClockCmd(LED4_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = LED4_PIN;
	GPIO_Init(LED4_GPIO_PORT, &GPIO_InitStructure);
#endif
}

/****************************************************
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
	
  /* Loop until the end of transmission */
	while (USART_GetFlagStatus(IAP_COM, USART_FLAG_TC) == RESET);
  USART_SendData(IAP_COM, (uint8_t) ch);

  return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

